<?php
/**
 * erp-品类日销量和库存-站点
 * (model)
 */
namespace App\Models\SalesReport;

use App\Models\Basics\SysPlat;
use App\Models\Product\ProductClassify;
use App\Models\Basics\SysArea;
use App\Models\Inventory\PublicCn;
use Illuminate\Database\Eloquent\Model;
class DailyCva extends Model
{
    protected $table = 'sr_daily_cva';

    protected $guarded = [];

    public $timestamps = false;

    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public function plat(){
        return $this->belongsTo(SysPlat::class,'plat_id','plat_id');
    }

    public static function headersField(){
        $fields = [];
        $fields['area'] = ['title' => '站点','sumField' => false];
        for($i = DailyCv::V_NUM; $i >= 1; $i--){
            $fields['day'.$i] = [
                'title' => date("m.d",strtotime("-$i days")),
                'sumField' => true
            ];
        }
        $fields['avg3'] = ['title' => '3天AVG','sumField' => true];
        $fields['avg7'] = ['title' => '7天AVG','sumField' => true];
        $fields['avg15'] = ['title' => '15天AVG','sumField' => true];
        $fields['avg30'] = ['title' => '30天AVG','sumField' => true];
        $fields['avg'] = ['title' => 'AVG','sumField' => false];
        $fields['sales_status'] = ['title' => '销量趋势','sumField' => false];
        $fields['fba_instant_num'] = ['title' => 'FBA在库','sumField' => true];
        $fields['fba_transit_num'] = ['title' => 'FBA在途','sumField' => true];
        $fields['cn_instant_num'] = ['title' => '国内在库','sumField' => true];
        $fields['cn_transit_num'] = ['title' => '国内在途','sumField' => true];
        $fields['os_instant_num'] = ['title' => '海外仓在库','sumField' => true];
        $fields['os_transit_num'] = ['title' => '海外仓在途','sumField' => true];
        $fields['inv_factor'] = ['title' => 'FBA&ALL库存系数','sumField' => false];
        return $fields;
    }

    public static function getDownloadData($re, $days, $startTime, $endTime, $timeMax,$platId,$classifyId,$season1,$reportStatus, $areaId){
        foreach($re as $item) {
            $title = ProductClassify::getTitleById($item->classify_id);
            $season = ProductClassify::$_season[$item->classify->season ?? 3];
            $temp = [];
            $temp['classify'] = $title;
            $temp['season'] = $season;
            $temp['plat'] = '';
            $temp['area'] = '';
            $dayVolumeArray = DailyVolume::getClassifyDaysVolume($startTime, $endTime, $item->plat_id, $item->classify_id,$areaId);
            $volume = 0;
            for($i = $days; $i >= 0; $i--){
                $dateTitle = date("Y-m-d", strtotime("$timeMax -$i day")); //日期格式 (2022-01-01)
                if(array_key_exists($dateTitle, $dayVolumeArray)){
                    $volume += $dayVolumeArray[$dateTitle]; //赋值,当期销量(当天)
                }
            }
            $temp['volume'] = $volume;
            //每日销量
            for($i = $days; $i >= 0; $i--){
                $dateTitle = date("Y-m-d", strtotime("$timeMax -$i day")); //日期格式 (2022-01-01)
                if(array_key_exists($dateTitle, $dayVolumeArray)){
                    $temp[$dateTitle] = $dayVolumeArray[$dateTitle]; //赋值,当期销量(当天)
                }else{
                    $temp[$dateTitle] = 0;
                }
            }
            $temp['avg3'] = round($item->avg3);
            $temp['avg7'] = round($item->avg7);
            $temp['avg15'] = round($item->avg15);
            $temp['avg30'] = round($item->avg30);
            $temp['avg'] = round($item->avg);
            $temp['sales_status'] = DailySv::$_salesStatus[$item->sales_status];
            if ($item->plat_id == SysPlat::PLAT_AMAZON) {
                $temp['cnp_instant_num'] = PublicCn::getInstantByClassify($item->classify_id); //公共在库
            }else{
                $temp['cnp_instant_num'] = 0; //公共在库
            }

            $temp['fba_instant_num'] = $item->fba_instant_num;
            $temp['fba_transit_num'] = $item->fba_transit_num;
            $temp['cn_instant_num'] = $item->cn_instant_num;
            $temp['cn_transit_num'] = $item->cn_transit_num;
            $temp['os_instant_num'] = $item->os_instant_num;
            $temp['os_transit_num'] = $item->os_transit_num;
            $temp['inventory_num'] = $item->fba_instant_num +
                                        $item->fba_transit_num +
                                        $item->cn_instant_num +
                                        $item->cn_transit_num +
                                        $item->os_instant_num +
                                        $item->os_transit_num; //库存(和)
            $temp['inv_factor'] = DailySv::getInvFactor($item);
            yield $temp;

            $platIds = $platId == null ? SysPlat::$_platformNormalId : [$platId];
            $cvIds = DailyCv::getDailyCvIds($platIds,$item->classify_id,$season1,$reportStatus);
            //销量(站点)
            $dailyCvaDataQuery = DailyCva::query()->whereIn('cv_id', $cvIds);
            if($areaId){
                $dailyCvaDataQuery->where('area_id', $areaId);
            }
            $dailyCvaData = $dailyCvaDataQuery->orderBy('plat_id')->get();
            foreach($dailyCvaData as $_item){
                $temp = [];
                $temp['classify'] = '';
                $temp['season'] = '';
                $temp['plat'] = $_item->plat->title ?? '';
                $temp['area'] = $_item->area->area ?? '';
                $areaDayVolumeArray = DailyVolume::getAreaClassifyDaysVolume($startTime, $endTime, $_item->plat_id, $_item->area_id, $item->classify_id);
                $areaDayTotal = 0;
                for ($i = $days; $i >= 0; $i--) {
                    $platDateTitle = $item->plat_id.'_'.date("Y-m-d", strtotime("$timeMax -$i day")); //赋值,日期格式 (1970-01-01)
                    if (array_key_exists($platDateTitle, $areaDayVolumeArray)) {
                        $areaDayTotal += $areaDayVolumeArray[$platDateTitle];
                    }
                }
                $temp['volume'] = $areaDayTotal;
                for ($i = $days; $i >= 0; $i--) {
                    $dateTitle = date("Y-m-d", strtotime("$timeMax -$i day")); //赋值,日期格式 (1970-01-01)
                    $platDateTitle = $item->plat_id.'_'.$dateTitle;
                    if (array_key_exists($platDateTitle, $areaDayVolumeArray)) {
                        $temp[$dateTitle] = $areaDayVolumeArray[$platDateTitle]; //销量(当天)
                    }else{
                        $temp[$dateTitle] = 0;
                    }
                }
                $temp['avg3'] = round($_item->avg3);
                $temp['avg7'] = round($_item->avg7);
                $temp['avg15'] = round($_item->avg15);
                $temp['avg30'] = round($_item->avg30);
                $temp['avg'] = round($_item->avg);
                $temp['sales_status'] = DailySv::$_salesStatus[$_item->sales_status];
                if ($_item->plat_id == SysPlat::PLAT_AMAZON) {
                    $temp['cnp_instant_num'] = PublicCn::getInstantByClassify($_item->classify_id); //公共在库
                }else{
                    $temp['cnp_instant_num'] = 0; //公共在库
                }
                $temp['fba_instant_num'] = $_item->fba_instant_num;
                $temp['fba_transit_num'] = $_item->fba_transit_num;
                $temp['cn_instant_num'] = $_item->cn_instant_num;
                $temp['cn_transit_num'] = $_item->cn_transit_num;
                $temp['os_instant_num'] = $_item->os_instant_num;
                $temp['os_transit_num'] = $_item->os_transit_num;
                $temp['inventory_num'] = $_item->fba_instant_num +
                                        $_item->fba_transit_num +
                                        $_item->cn_instant_num +
                                        $_item->cn_transit_num +
                                        $_item->os_instant_num +
                                        $_item->os_transit_num; //库存(和)
                $temp['inv_factor'] = DailySv::getInvFactor($_item);
                yield $temp;
            }
        }
    }
}
